﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;

namespace MyVocationSpace
{
    public partial class MyDashboard : System.Web.UI.Page
    {
        public int CurrentPage = 0;
        public int totalPage;
        public int pageNo = 1;
        public DataSet datasource;

        public int CurrentPage1 = 0;
        public int totalPage1;
        public int pageNo1 = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] == null)
            {
                Response.Redirect("Home.aspx");
            }

            if (Session["UserID"] != null)
            {
                if (!IsPostBack)
                {   
                    BAL.Users.PopulateMyVocation(ref ddlVocation, Convert.ToInt32(Session["UserID"]));
                    BAL.Industry.PopulateCountry(ref ddlCountry);
                    //ddlCountry.SelectedValue = Session["IDCountry"].ToString();

                    string[] detail = BAL.Users.GetUserDetails(Convert.ToInt32(Session["UserID"]));
                    ListItem lstCountry = ddlCountry.Items.FindByValue(detail[15]);
                    if (lstCountry != null)
                    {
                        ddlCountry.SelectedValue = lstCountry.Value;
                    }

                    DataSet ds = BAL.Users.GetIndustryByUser(Convert.ToInt32(Session["UserID"]));
                    lblIndustryName.Text = ds.Tables[0].Rows[0][0].ToString();
                    hdnIDIndustry.Value = ds.Tables[0].Rows[0][1].ToString();
                    ViewState["hdnIDIndustry"] = hdnIDIndustry.Value;

                    // lblIndustryName.Text = BAL.Users.GetIndustryByUser(Id);


                    PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ddlCountry.SelectedValue), 0, Convert.ToInt32(ViewState["hdnIDIndustry"])));
                    PopulateTopic(Convert.ToInt32(Session["UserID"]), "", Convert.ToInt32(ViewState["hdnIDIndustry"]), 0, Convert.ToInt32(ddlCountry.SelectedValue));


                    //if (ddlVocation.SelectedValue != "-1")
                    //{
                    //    PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ddlCountry.SelectedValue), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ViewState["hdnIDIndustry"])));
                    //}
                    //else
                    //{
                    //    PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["IDCountry"]), 0, Convert.ToInt32(Session["IDIndustry"])));
                    //}


                    //if (txtsearchtopic.Text == "Enter Topic Name")
                    //{
                    //    PopulateTopic(Convert.ToInt32(Session["UserID"]), "", Convert.ToInt32(ViewState["hdnIDIndustry"]), 0, Convert.ToInt32(Session["IDCountry"]));
                    //}
                    //else
                    //{
                    //    PopulateTopic(Convert.ToInt32(Session["UserID"]), txtsearchtopic.Text, Convert.ToInt32(ViewState["hdnIDIndustry"]), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ddlCountry.SelectedValue));
                    //}
                }

            }
        }
        public void PopulateMymemberList(DataSet ds)
        {
            DataSet dsVocationMem = ds;
            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            int noOfItem = pgds.Count;
            pgds.AllowPaging = true;
            pgds.PageSize = 10;

            totalPage = pgds.PageCount;
            ViewState["TotalPage"] = totalPage;

            pgds.CurrentPageIndex = CurrentPage;
            ViewState["CurrentPage"] = CurrentPage;

            lbtnNext.Visible = !pgds.IsLastPage;
            lbtnPrev.Visible = !pgds.IsFirstPage;

            int CurrentPageShow = Convert.ToInt32(ViewState["CurrentPage"]) + 1;

            spanIdVocMember.Style.Add("display", "none");
            Label1.Visible = true;
            Label2.Visible = true;

            Label1.Text = "Page " + CurrentPageShow.ToString() + " of " + ViewState["TotalPage"].ToString();
            Label2.Text = noOfItem + " items found. Showing upto 10 items per page.";

            if (pgds.DataSource != null)
            {
                //if (pgds.DataSourceCount > 0)
                //{

                dlMyVocationMember.DataSource = pgds.DataSource;
                dlMyVocationMember.DataBind();
                //}
            }
            else
            {
                spanIdVocMember.Style.Add("display", "block");
                spanIdVocMember.InnerHtml = "Data Not Found...!!!";
            }



            //if (ds.Tables.Count > 0)
            //{
            //    if (ds.Tables[0].Rows.Count > 0)
            //    {

            //        foreach (DataRow dr in ds.Tables[0].Rows)
            //        {
            //            phMemebers.Controls.Add(new LiteralControl("<table    class=\"light-border MemberList\"><tr><td align=\"Center\"   >"));
            //            phMemebers.Controls.Add(new LiteralControl("<div style=\"float: left; padding: 2px;width:70px\"><a href=\"MyProfile.aspx?IDUser=" + dr["IDUser"] + "\">"));
            //            if (!string.IsNullOrEmpty(dr["Photo"].ToString()))
            //            {
            //                phMemebers.Controls.Add(new LiteralControl("<img src=\"UsersPhoto/" + dr["Photo"].ToString() + "\" style=\"width: 60px; height: 60px;border: 0px\" alt=\"\" /></a>"));

            //            }
            //            else
            //            {
            //                phMemebers.Controls.Add(new LiteralControl("<img src=\"UsersPhoto/icon_no_photo_60x60.png\" style=\"width: 60px; height: 60px;border: 0px\" alt=\"\" /></a>"));
            //            }
            //            phMemebers.Controls.Add(new LiteralControl("</div>"));
            //            //main
            //            phMemebers.Controls.Add(new LiteralControl("<div style=\"float: left; width: 445px;text-align:left\">"));
            //            //for name
            //            phMemebers.Controls.Add(new LiteralControl("<div  id=\"membername\" ><a href=\"MyProfile.aspx?IDUser="+dr["IDUser"].ToString()+"\" >" + dr["Firstname"].ToString() + " " + dr["Lastname"].ToString() + "</a></div>"));
            //           //for position
            //            phMemebers.Controls.Add(new LiteralControl("<div class=\"medium-msg-black\"> " + dr["CurrentPosition"].ToString() + "  at <span>" + dr["Orgnization"].ToString() + "</span></div>"));

            //            //for country and industry
            //            phMemebers.Controls.Add(new LiteralControl("<div class=\"medium-msg-light\">"));
            //            phMemebers.Controls.Add(new LiteralControl("<span>" + dr["CountryName"].ToString() + "</span>,<span><b>" + dr["IndustryName"].ToString() + "</b></span>"));
            //            phMemebers.Controls.Add(new LiteralControl("</div>"));

            //            //connections
            //            phMemebers.Controls.Add(new LiteralControl("<div class=\"\">"));
            //            phMemebers.Controls.Add(new LiteralControl("</div>"));


            //            phMemebers.Controls.Add(new LiteralControl("</div>"));
            //           //main end
            //            phMemebers.Controls.Add(new LiteralControl("</td></tr></table>"));
            //        }

            //    }
            //}

        }

        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage -= 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo -= 1;

            if ((ddlCountry.SelectedValue == Convert.ToString(ViewState["IdCountrySearch"])) || (ddlVocation.SelectedValue == Convert.ToString(ViewState["newVoc"])))
            {
                PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ViewState["IdCountrySearch"]), Convert.ToInt32(ViewState["newVoc"]), Convert.ToInt32(ViewState["hdnIDIndustry"])));
            }
            else
            {
                PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ddlCountry.SelectedValue), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ViewState["hdnIDIndustry"])));

            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage += 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo += 1;

            if ((ddlCountry.SelectedValue == Convert.ToString(ViewState["IdCountrySearch"])) || (ddlVocation.SelectedValue == Convert.ToString(ViewState["newVoc"])))
            {
                PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ViewState["IdCountrySearch"]), Convert.ToInt32(ViewState["newVoc"]), Convert.ToInt32(ViewState["hdnIDIndustry"])));
            }
            else
            {
                PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ddlCountry.SelectedValue), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ViewState["hdnIDIndustry"])));

            }
        }

        protected void dlMyVocationMember_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlImage imgHtml = (HtmlImage)e.Item.FindControl("imgPerson");
                Label lblCon = (Label)e.Item.FindControl("lblConnection");

                int IDUser = Convert.ToInt32(dlMyVocationMember.DataKeys[e.Item.ItemIndex]);
                ViewState["IDUser"] = IDUser;

                string img = imgHtml.Src;

                if (imgHtml.Src == "")
                {
                    imgHtml.Src = "UsersPhoto/icon_no_photo_60x60.png";
                }
                else
                {
                    imgHtml.Src = "UsersPhoto/" + img;
                }

                int count = BAL.Users.GetConnectionByUser(IDUser);
                lblCon.Text = count.ToString() + " " + "<font style=\"color:#1C6F93; font-size:12px; font-weight:normal;\">connections</font>";

                LinkButton lnkConnectSend = (LinkButton)e.Item.FindControl("lnkConnectSend");
                HtmlGenericControl spanMYConnection = (HtmlGenericControl)e.Item.FindControl("spanMYConnection");

                HtmlInputHidden hdn = (HtmlInputHidden)e.Item.FindControl("hiddenIDConnectionStatus");

                HtmlGenericControl idsuccesstick = (HtmlGenericControl)e.Item.FindControl("idsuccesstick");
                HtmlGenericControl spanSuccessMsg = (HtmlGenericControl)e.Item.FindControl("spanSuccessMsg");
                //HtmlGenericControl spnsentMsgTick = (HtmlGenericControl)e.Item.FindControl("spnsentMsgTick");
                HtmlGenericControl spnsentMsg = (HtmlGenericControl)e.Item.FindControl("spnsentMsg");

                if (hdn.Value == "1")
                {
                    lnkConnectSend.Visible = false;
                    spanMYConnection.Visible = false;

                    idsuccesstick.Visible = true;
                    spanSuccessMsg.InnerHtml = "<font style=\"color:green; font-size:12px; font-weight:bold;\">Connection has been sent</font>";
                }
                else if (hdn.Value == "2")
                {
                    lnkConnectSend.Visible = false;
                    spanMYConnection.Visible = false;
                    idsuccesstick.Visible = true;
                    spanSuccessMsg.InnerHtml = "<font style=\"color:green; font-size:12px; font-weight:bold;\">Connected</font>";
                }
                else if (hdn.Value == "" || hdn.Value == "NULL")
                {
                    lnkConnectSend.Visible = true;
                    spanMYConnection.Visible = true;
                }

                else
                {
                    lnkConnectSend.Visible = true;
                    spanMYConnection.Visible = true;
                }
                lnkConnectSend.OnClientClick = "SendConnectionReq(" + IDUser + ",'" + spnsentMsg.ClientID + "', '" + spanMYConnection.ClientID + "','" + lnkConnectSend.ClientID + "');return false;";


            }
        }


        public void PopulateTopic(int IDUser, string str, int IDIndustry, int IDVocation, int IDCountry)
        {
            DataSet ds = BAL.Users.GetTopicOfMyVocation(IDUser, str, IDIndustry, IDVocation, IDCountry);
            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            int noOfItem = pgds.Count;
            pgds.AllowPaging = true;
            pgds.PageSize = 10;

            totalPage1 = pgds.PageCount;
            ViewState["TotalPage1"] = totalPage1;

            pgds.CurrentPageIndex = CurrentPage1;
            ViewState["CurrentPage1"] = CurrentPage1;

            lnkNextTopic.Visible = !pgds.IsLastPage;
            lnkPreTopic.Visible = !pgds.IsFirstPage;

            int CurrentPageShow = Convert.ToInt32(ViewState["CurrentPage1"]) + 1;
            Label3.Text = "Page " + CurrentPageShow.ToString() + " of " + ViewState["TotalPage1"].ToString();
            Label4.Text = noOfItem + " items found. Showing upto 10 items per page.";

            if (pgds.DataSource != null)
            {
                dlVocationTopic.DataSource = pgds.DataSource;
                dlVocationTopic.DataBind();
            }

        }



        protected void lnkPreTopic_Click(object sender, EventArgs e)
        {
            CurrentPage1 = Convert.ToInt32(ViewState["CurrentPage1"]);
            CurrentPage1 -= 1;
            pageNo1 = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo1 -= 1;

            if (txtsearchtopic.Text == "Enter Topic Name")
            {
                PopulateTopic(Convert.ToInt32(Session["UserID"]), "", Convert.ToInt32(ViewState["hdnIDIndustry"]), 0, Convert.ToInt32(Session["IDCountry"]));
            }
            else
            {
                PopulateTopic(Convert.ToInt32(Session["UserID"]), txtsearchtopic.Text, Convert.ToInt32(ViewState["hdnIDIndustry"]), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ddlCountry.SelectedValue));
            }
        }

        protected void lnkNextTopic_Click(object sender, EventArgs e)
        {
            CurrentPage1 = Convert.ToInt32(ViewState["CurrentPage1"]);
            CurrentPage1 += 1;
            pageNo1 = Convert.ToInt32(ViewState["CurrentPage1"]);
            pageNo1 += 1;

            if (txtsearchtopic.Text == "Enter Topic Name")
            {
                PopulateTopic(Convert.ToInt32(Session["UserID"]), "", Convert.ToInt32(ViewState["hdnIDIndustry"]), 0, Convert.ToInt32(Session["IDCountry"]));
            }
            else
            {
                PopulateTopic(Convert.ToInt32(Session["UserID"]), txtsearchtopic.Text, Convert.ToInt32(ViewState["hdnIDIndustry"]), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ddlCountry.SelectedValue));
            }
        }

        protected void btnSubmitsearch_Click(object sender, EventArgs e)
        {
            int IdVocation = Convert.ToInt32(ddlVocation.SelectedValue);
            int newVoc = 0;
            if (IdVocation == -1)
            {
                newVoc = 0;
            }
            else
            {
                newVoc = IdVocation;
            }

            ViewState["newVoc"] = newVoc;

            int IdCountry = Convert.ToInt32(ddlCountry.SelectedValue);
            ViewState["IdCountrySearch"] = IdCountry;
            string strSearch = txtsearchtopic.Text;

            if (strSearch == "Enter Topic Name")
            {
                strSearch = "";
            }
            PopulateTopic(Convert.ToInt32(Session["UserID"]), strSearch, Convert.ToInt32(ViewState["hdnIDIndustry"]), newVoc, IdCountry);
            PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), IdCountry, IdVocation, Convert.ToInt32(ViewState["hdnIDIndustry"])));
        }


        protected void lnkConnectSend_Click(object sender, EventArgs e)
        {
            //LinkButton lnk = sender as LinkButton;
            //if (lnk != null)
            //{
            //    int id = Convert.ToInt32(lnk.CommandArgument);
            //    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "SendConnectionReq(" + id + ");", true);
            //}

        }

        protected void ShowFooter_OnPreRender(object sender, EventArgs e)
        {
            DataList dlObj = (DataList)sender;
            if (dlObj.Items.Count == 0)
            {
                dlObj.Visible = true;

                spanIdVocMember.Style.Add("display", "block");
                spanIdVocMember.InnerHtml = "Data Not Found...!!!";
                Label1.Visible = false;
                Label2.Visible = false;

            }
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSearch = txtsearchtopic.Text;
            if (strSearch == "Enter Topic Name")
            {
                strSearch = "";
            }
            PopulateMymemberList(BAL.Users.MembersByVocation(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(ddlCountry.SelectedValue), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ViewState["hdnIDIndustry"])));
            //PopulateTopic(Convert.ToInt32(Session["UserID"]), strSearch, Convert.ToInt32(ViewState["hdnIDIndustry"]), Convert.ToInt32(ddlVocation.SelectedValue), Convert.ToInt32(ddlCountry.SelectedValue));
        }

    }
}